

** make a table of bounds

/*


load and collapse to weekly sums
save as tempory files
stitch together 


*/
clear 
scalar pop = 6.6e6
timer clear 1
timer on 1 
qui forvalues bs = 0 /500 {
	noi di "on `bs'"
	********************************************************************************
	********************************************************************************
	** load and save all tests
	********************************************************************************
	********************************************************************************
	** all tests
	use health/test_week 

	** resample 
	if `bs' > 0 bsample _N
	
	** aggregate, reweight 
	collapse (sum) tested positive , by(week)	

	gen test_rate = tested / pop
	gen conf_pos_rate = positive / pop
	gen pos_rate = positive / tested

	gen group ="All"
	save health/all_weekly_unwtd_bs, replace 
		
	********************************************************************************
	********************************************************************************
	** load and save the hospital tests, by group
	********************************************************************************
	********************************************************************************
	foreach group in  no_icli any_major any_icli {
		
		* load the data
		use health/inp_`group', clear 							
		** resample 
		if `bs' > 0 bsample _N
			
		collapse (sum) test_tight pos_tight (count) n=test_tight, 	by(week)
		gen test_rate = test_tight / n
		gen conf_pos_rate = pos_tight / n
		gen pos_rate = pos_tight / test_tight 

		
		gen group = "`group'"
		save health/`group'_weekly_unwtd_bs, replace

		
	}

	********************************************************************************
	********************************************************************************
	** append together etc
	********************************************************************************
	********************************************************************************

	clear
	foreach group in all any_icli no_icli any_major {
		append using health/`group'_weekly_unwtd_bs
	}

	gen groupn = 1 if group == "All"
	replace groupn = 2 if group == "no_icli"
	replace groupn = 3 if group == "any_major"
	replace groupn = 4 if group == "any_icli"
	replace groupn = 5 if group == "pool_icli"

	rename conf_pos_rate lower_bound
	rename pos_rate upper_bound

	keep n test_rate group lower_bound upper_bound week groupn
	reshape wide n test_rate group lower_bound upper_bound, i(week) j(groupn)
	sort week  
	
	
	gen bs = `bs'
	# delimit ;
	keep bs week  
		lower_bound1 upper_bound1 
		lower_bound2 upper_bound2 
		lower_bound3 upper_bound3 
		lower_bound4 upper_bound4 
	;
	# delimit cr
	save bootstrap_ests/bs_u_`bs', replace 
}

timer off 1
timer list 1 

use bootstrap_ests/bs_u_0, clear 

format lower_bound*  %10.4f
format upper_bound* %10.3f
list week lower_bound1 upper_bound1 lower_bound2 upper_bound2 in 1/2


